/**
 * Copyright (c) 2018 qar All rights reserved.
 *
 * https://www.qar.io
 *
 * 版权所有，侵权必究！
 */

package com.example.controller;

import com.example.dto.SysDictTypeDTO;
import com.example.entity.DictType;
import com.example.feign.DictFeignClient;
import com.example.model.api.Result;
import com.example.model.page.PageData;
import com.example.service.SysDictTypeService;
import com.example.utils.ConvertUtils;
import com.example.validator.AssertUtils;
import com.example.validator.ValidatorUtils;
import com.example.validator.group.DefaultGroup;
import com.example.validator.group.UpdateGroup;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 字典类型
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("dict/type")
@Tag(name = "字典类型")
public class SysDictTypeController implements DictFeignClient {
    @Autowired
    private SysDictTypeService sysDictTypeService;

    @GetMapping("page")
    @Operation(summary = "分页")
    public Result<PageData<SysDictTypeDTO>> page(@RequestParam Map<String, Object> params) {
        PageData<SysDictTypeDTO> page = sysDictTypeService.page(params);
        return new Result<PageData<SysDictTypeDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    public Result<SysDictTypeDTO> get(@PathVariable("id") Long id) {
        SysDictTypeDTO data = sysDictTypeService.get(id);
        return new Result<SysDictTypeDTO>().ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
    public Result<String> save(@RequestBody SysDictTypeDTO dto) {
        ValidatorUtils.validateEntity(dto, DefaultGroup.class);
        sysDictTypeService.save(dto);
        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    public Result<String> update(@RequestBody SysDictTypeDTO dto) {
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        sysDictTypeService.update(dto);
        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    public Result<String> delete(@RequestBody Long[] ids) {
        AssertUtils.isArrayEmpty(ids, "数组参数不能为空");
        sysDictTypeService.delete(ids);
        return Result.ok();
    }

    @GetMapping("all")
    @Operation(summary = "所有字典数据")
    public Result<List<DictType>> all() {
        List<DictType> list = sysDictTypeService.getAllList();
        return new Result<List<DictType>>().ok(list);
    }

    @PostMapping("list")
    @Operation(summary = "列表")
    public Result<List<SysDictTypeDTO>> list() {
        List<DictType> list = sysDictTypeService.getDictTypeList();
        List<SysDictTypeDTO> typeList = ConvertUtils.sourceToTarget(list, SysDictTypeDTO.class);
        return new Result<List<SysDictTypeDTO>>().ok(typeList);
    }

    @Override
    @GetMapping("getDictTypeList")
    public Result<List<SysDictTypeDTO>> getDictTypeList() {
        List<DictType> list = sysDictTypeService.getDictTypeList();
        List<SysDictTypeDTO> typeList = ConvertUtils.sourceToTarget(list, SysDictTypeDTO.class);
        return new Result<List<SysDictTypeDTO>>().ok(typeList);
    }
}